#!/bin/bash

# HOST="13.127.67.158"
HOST="3.109.15.99"
KEY="/Users/hi/.ssh/id_rsa"
GAME=$1
ENV=$2
# app名字 有yoyo,zclub
APPNAME=$3
PWD=$(pwd)
BASE=$(basename $PWD)

# 各个游戏发布对应的cdn ludo uno dominoe slots Slots
gameUrlMap=(["slot_aht"]="res.game.zclub.app", ["ludo"]="a.fslk.co/games", ["dominoe"]="a.fslk.co/games", ["uno"]="a.fslk.co/games", ["Slots"]="a.fslk.co/games")
zclubGameList=("slot_aht" "crazylion" "holdem")
# ENV="staging"

echo "GAME is $GAME"

# web-mobile文件夹下的路径
DIRABSPATH=$(greadlink -f ../)
TARGETDIR="${HOME}/Desktop/allWebmobile/"

echo "================Start deal zip files=================="
cd ..
zipFileIsExist=0

# Check if the zip file exist
if [[ -f "web-mobile.zip" ]]; then
    zipFileIsExist=1
else
    zip -r -q web-mobile.zip web-mobile
fi

isInZclubGameList() {
    listLen=${#zclubGameList[@]}
    echo "$listLen"

    for ((i = 0; i < $listLen; i++)); do
        if [ ${zclubGameList[i]} = $GAME ]; then
            echo "找到目标游戏"
            return 1
        fi
    done
    return 0
}

movZipFileToTarget() {
    zipFile=$DIRABSPATH"/web-mobile.zip"
    echo "Path of zipFile is "$zipFile
    echo "    ==============Start move zip File==============="
    # mv
    targetDir=$TARGETDIR$GAME"/"$ENV
    if [[ ! -d $targetDir ]]; then
        echo "不存在"$targetDir"文件夹,创建该文件夹"
        mkdir "$targetDir"
    fi

    # if [[ -f "$targetDir/web-mobile.zip" ]]; then
    #     # delete old web-mobile.zip
    #     echo "存在zip旧文件"
    #     # rm -rf "$targetDir/web-mobile.zip"
    # fi
    # Move the zipFile to target path
    mv $zipFile $targetDir
    # Rename zipFile by current time
    curTime=$(date "+%Y%m%d%H%M%S")
    mv "$targetDir/web-mobile.zip" "$targetDir/web-mobile.$curTime.zip"
    echo "    ==============Move zip File over==============="
}

movZipFileToTarget

# Zip web-mobile dir
zip -r web-mobile.zip web-mobile/
echo "    ===========zipFileIsExist is $zipFileIsExist========"
if [[ $zipFileIsExist == 0 ]]; then
    # It's no any zip file before zip opration
    movZipFileToTarget
fi
cd web-mobile/
echo "================Deal zip files over=================="

if [[ $BASE != "web-mobile" ]]; then
    echo "请在 web-mobile 目录下执行"
    exit 0
fi
echo "当前文件夹 is " $DIRABSPATH

if [[ $GAME == "" ]]; then
    echo "参数不正确(1), 示范: publish ludo staging"
    exit 1
fi

if [[ $APPNAME == "" ]]; then
    echo "app name error,input yoyo or zclub"
    exit 1
fi

if [[ $APPNAME != "yoyo" && $APPNAME != "zclub" && $APPNAME != 'u2' ]]; then
    echo "app 参数不正确"
    exit 0
fi

if [[ $ENV != "staging" && $ENV != "prod" ]]; then
    echo "参数不正确(2), 示范: publish ludo staging"
    exit 0
fi

echo "确定发布" $GAME/$ENV/web-mobile "吗? 回答: yes/no"
read

if [[ $REPLY != "yes" ]]; then
    exit 0
fi

# 自动上传s3
uploadS3() {
    echo "ENV is $ENV"
    echo "GAME is $GAME"
    isInZclubGameList $GAME
    isIn=$?
    echo $isIn
    if [[ $APPNAME == "zclub" ]]; then
        echo "zclub项目"
        if [[ $GAME == "slot_aht" ]]; then
            # if [[ $APPNAME == "yoyo" ]]; then
            #     GAME="Slot"
            # else
            #     GAME="slots"
            # fi
            GAME="slots"
        fi

        echo "开始同步 s3://res.game.zclub.app/$GAME/$ENV"
        /usr/local/bin/rsync -av --delete ./ $HOST:~/web-mobile
        echo
        echo "开始发布 s3://res.game.zclub.app/$GAME/$ENV"
        echo
        aws s3 sync ./ s3://res.game.zclub.app/$GAME/$ENV
        echo "publish over"
        echo "http://res.game.zclub.app/$GAME/$ENV/index.html"
        echo "clear CDN cache"
        nd purge -d "[\"http://res.game.zclub.app/$GAME/$ENV/index.html\"]"
        #         ssh $HOST <<eeooff
        #             cdn_purge "http://res.game.zclub.app/$GAME/$ENV/index.html"
        #             exit
        # eeooff
    elif [[ $APPNAME == "u2" ]]; then
        echo "u2项目"
        # if [[ $GAME == "slot_aht" ]]; then
        #     # if [[ $APPNAME == "yoyo" ]]; then
        #     #     GAME="Slot"
        #     # else
        #     #     GAME="slots"
        #     # fi
        #     GAME="Slot"
        # fi
        echo "开始同步 s3://a.game.amurmur.app/$GAME/$ENV"
        /usr/local/bin/rsync -av --delete ./ $HOST:~/web-mobile

        # 完成上线发布
        ssh -i $KEY "lichengkun@"$HOST <<eeooff
        cd ~/web-mobile
        echo
        echo "开始发布 s3://a.game.amurmur.app/$GAME/$ENV"
        echo
        aws --profile yoyo s3 sync ./ s3://a.game.amurmur.app/$GAME/$ENV
        echo "发布完毕"
        echo "清理 CDN"
        echo
        exit
eeooff
        nd purge -d "[\"http://a.game.amurmur.app/$GAME/$ENV/index.html\"]"
    else
        echo "yoyo项目"
        if [[ $GAME == "slot_aht" ]]; then
            # if [[ $APPNAME == "yoyo" ]]; then
            #     GAME="Slot"
            # else
            #     GAME="slots"
            # fi
            GAME="Slot"
        fi
        echo "开始同步 s3://a.fslk.co/games/$GAME/$ENV/web-mobile"
        /usr/local/bin/rsync -av --delete ./ $HOST:~/web-mobile

        # 完成上线发布
        ssh -i $KEY "lichengkun@"$HOST <<eeooff
        cd ~/web-mobile
        echo
        echo "开始发布 s3://a.fslk.co/games/$GAME/$ENV/web-mobile"
        echo
        aws --profile yoyo s3 sync ./ s3://a.fslk.co/games/$GAME/$ENV/web-mobile
        echo "发布完毕"
        echo "清理 CDN"
        echo
        exit
eeooff
        nd purge -d "[\"http://a.fslk.co/games/$GAME/$ENV/web-mobile/index.html\"]"

    fi
}

uploadS3
